﻿package domino 
{
	import Box2D.Common.Math.b2Vec2;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.geom.Point;
	import flash.display.Sprite;
	import Box2D.Dynamics.Joints.*;

	import Box2D.Dynamics.*;
	import Box2D.Collision.*;
	import Box2D.Collision.Shapes.*;
	
	/**
	 * ...
	 * @author ...
	 */
	public class actorAngle extends actor
	{
		
		private var final_body:b2Body;
		private	var the_body:b2BodyDef;
		private	var the_box:b2PolygonDef;
		
		public var stat:Number;
		public var dominSprite:Sprite
		public var scaleX:Number, scaleY:Number;
		public var density:Number, angV:Number;
		public var rev_joint:b2Body;
		public function actorAngle(_world:b2World,parent:DisplayObjectContainer,dominPosition:Point,_rot:Number,_density:Number,_friction:Number,_scaleX:Number,_scaleY:Number,_restitution:Number,_angV:Number,isSensor:int) 
		{
			//create costume
			dominSprite= new angle2();
			dominSprite.scaleX = _scaleX;
			dominSprite.scaleY = _scaleY;
			scaleX = _scaleX;
			scaleY = _scaleY;
			density = _density;
			angV = _angV;
			dominSprite.x = dominPosition.x;
			dominSprite.y = dominPosition.y;
       	    parent.addChild(dominSprite);
		
			//create body
			//pivot
			var the_pivot:b2CircleDef = new b2CircleDef();
			the_pivot.radius = .01;
			the_pivot.density = 0;
			the_pivot.restitution = _restitution;
			the_pivot.friction = 1;
			the_pivot.isSensor = Boolean(isSensor);
			
			var bd:b2BodyDef = new b2BodyDef();
			bd.position.Set(dominSprite.x/30,dominSprite.y/30);
			rev_joint = _world.CreateBody(bd);

			rev_joint.CreateShape(the_pivot)
			rev_joint.SetMassFromShapes();
		    //////// 
			the_body = new b2BodyDef();
			the_body.position.Set(dominSprite.x / 30 , dominSprite.y / 30 );
			the_body.angle = _rot * Math.PI / 180;
			final_body=_world.CreateBody(the_body);
			///////1
			the_box = new b2PolygonDef();
			the_box.vertexCount = 4;
	        the_box.vertices[0].Set(0,0);
			the_box.vertices[1].Set(dominSprite.width/30,0);
			the_box.vertices[2].Set(dominSprite.width/30,dominSprite.width/180);
			the_box.vertices[3].Set(0,dominSprite.width / 180);
			the_box.friction = _friction;	
			the_box.density = _density;
			the_box.restitution = _restitution;
			the_box.userData = rev_joint;
			the_box.isSensor = Boolean(isSensor);
			final_body.CreateShape(the_box);
			
			the_box.vertexCount = 4;
			the_box.vertices[0].Set(0,0);
			the_box.vertices[1].Set(dominSprite.width/180,0);
			the_box.vertices[2].Set(dominSprite.width/180,dominSprite.width/30);
			the_box.vertices[3].Set(0,dominSprite.width/30);
			the_box.friction = _friction;	
			the_box.density = _density;
			the_box.restitution = _restitution;
			the_box.userData = rev_joint;
			the_box.isSensor = Boolean(isSensor);
			final_body.CreateShape(the_box);
            final_body.AllowSleeping(true);
			final_body.SetXForm(new b2Vec2(final_body.GetPosition().x,final_body.GetPosition().y), _rot * Math.PI/180);
			final_body.SetMassFromShapes();
			
			var the_rev_joint:b2RevoluteJointDef = new b2RevoluteJointDef();
			the_rev_joint.enableMotor=true;
			the_rev_joint.motorSpeed=angV;
			the_rev_joint.maxMotorTorque=10000;
		
			the_rev_joint.Initialize(rev_joint, final_body, new b2Vec2(dominSprite.x / 30, dominSprite.y / 30));
			var joint_added:b2RevoluteJoint = _world.CreateJoint(the_rev_joint) as b2RevoluteJoint;
			
			
			super(final_body, dominSprite);
			setMovie(density);
		}
		
	 public function getData():Array {
		
		var data:Array = new Array();	
		data.push(int(_costume.x));
		data.push(int(_costume.y));
		data.push(Number(dominSprite.scaleX));
		data.push(Number(dominSprite.scaleY));
		data.push(Number(_body.GetShapeList().GetFriction()));
		data.push(Number(_body.GetShapeList().m_density));
	    data.push(Number(_costume.rotation));
		data.push(Number(_body.GetShapeList().GetRestitution()));	
		data.push(Number(angV));
		return data;
	}	
			
	 private function setMovie(s:Number):void {
			if(s==0)
			 MovieClip(_costume).gotoAndStop(2);
			else
			 MovieClip(_costume).gotoAndStop(1);
			 
			}
	}

}